package com.cn.yz.service.sysauthority.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpStatus;

import com.cn.yz.common.exception.BusinessException;
import com.cn.yz.common.exception.ErrMessage;
import com.cn.yz.common.httpentity.ResponseErr;

@ControllerAdvice
//如果返回的为json数据或其它对象，添加该注解  
@ResponseBody
public class GlobalExceptionHandler {
	
	private static Logger LOG = LoggerFactory.getLogger(GlobalExceptionHandler.class);

	/**
	 * 业务异常
	 * @param request
	 * @param exception
	 * @return
	 * @throws Exception
	 */
	@ExceptionHandler(value=BusinessException.class)
	public ResponseErr handlebusinessException(BusinessException exception) throws Exception{
		//解析原错误信息，封装后返回，此处返回非法的字段名称，原始值，错误信息  
		LOG.error("GlobalExceptionHandler.businessExceptionHandler--->" + exception.getErrMssage().getMessage());
		return new ResponseErr(exception.getErrMssage());
	}
	
	/**
	 * 400 - Bad Request 
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.BAD_REQUEST)
	@ExceptionHandler(HttpMessageNotReadableException.class)
	public ResponseErr handleHttpMessageNotReadableException(HttpMessageNotReadableException e) {
		LOG.error("GlobalExceptionHandler.handleHttpMessageNotReadableException--->" + e);
		return new ResponseErr(ErrMessage.COULD_NOT_READ_JSON);
	}
	
	/**
	 * 405 - Method Not Allowed
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	public ResponseErr handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		LOG.error("GlobalExceptionHandler.handleHttpRequestMethodNotSupportedException--->" + e);
		return new ResponseErr(ErrMessage.REQUEST_METHOD_NOT_SUPPORTED);
	}
	
	/**
	 * 415 - Unsupported Media Type 
	 * @param e
	 * @return
	 */
	@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
	@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
	public ResponseErr handleHttpMediaTypeNotSupportedException(Exception e) {
		LOG.error("GlobalExceptionHandler.handleHttpMediaTypeNotSupportedException--->" + e);
		return new ResponseErr(ErrMessage.CONTENT_TYPE_NOT_SUPPORTED);
	}
	
	/**
	 * 500 - Internal Server Error
	 * @param e
	 * @return
	 */
	/*@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
	@ExceptionHandler(Exception.class)
	public ResponseErr handleException(Exception e) {
		LOG.error("GlobalExceptionHandler.handleException--->" + e);
		return new ResponseErr("500",e.getMessage());
	}*/
}
